﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Main_Inventory_IssueItemShortClose : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {            
            DivIssueItemShortClose.Visible = true;
            GetDataFromIssueItem();
        }
    }
    protected void GetDataFromIssueItem()
    {
        string sqlIssueitem = "select RegId,slipno,username,Issued_By,Issued_to,ShortcloseDate,shortclose_remark,ISNULL(shortclose,'0') as ItemShortClose from IVR_OnlineRegistrationSlip";
        DataTable DTIssue = DU.getdata(sqlIssueitem);
        if (DTIssue.Rows.Count > 0)
        {
            grdIssueItemShortClose.DataSource = DTIssue;
            grdIssueItemShortClose.DataBind();
        }
    }
    protected void grdIssueItemShortClose_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkitemissue = (CheckBox)e.Row.FindControl("ChkItem");
            if (chkitemissue.Checked == true)
            {
                chkitemissue.Enabled = false;
            }
            HiddenField lblregidNo=(HiddenField)e.Row.FindControl("hiditemissue");
            string SqlSlipNo = "select * From IVR_OnlineRegistration_Detail where RegId=" + lblregidNo.Value;
            GridView GrdPOChild = (GridView)e.Row.FindControl("gvPOShortChild");
            DataTable DtItemDetails = DU.getdata(SqlSlipNo);
            if (DtItemDetails.Rows.Count > 0)
            {
                DataTable DTChildPO = new DataTable();
                DTChildPO.Columns.Add("ITEMCODE", typeof(string));
                DTChildPO.Columns.Add("ITEMDESC", typeof(string));
                //DTChildPO.Columns.Add("REVISIONREMARK", typeof(string));
                //DTChildPO.Columns.Add("REVISIONORDER", typeof(string));
                //DTChildPO.Columns.Add("REJECTREMARK", typeof(string));
                //DTChildPO.Columns.Add("CDATE", typeof(string));
                //DTChildPO.Columns.Add("CTIME", typeof(string));
                if (DtItemDetails.Rows.Count > 0)
                {
                    for (int i = 0; i < DtItemDetails.Rows.Count; i++)
                    {
                        DataRow DR = DTChildPO.NewRow();                        
                        //DR["POSTAGE"] = "Admin";                        
                        DR["ITEMCODE"] = DtItemDetails.Rows[i]["ITEMCODE"].ToString();
                        DR["ITEMDESC"] = DtItemDetails.Rows[i]["ITEMDESC"].ToString();
                        //DR["REVISIONREMARK"] = DtItemDetails.Rows[i]["REVISIONREMARK"].ToString();
                        //DR["REVISIONORDER"] = DtItemDetails.Rows[i]["REVISIONORDER"].ToString();
                        //DR["REJECTREMARK"] = DtItemDetails.Rows[i]["REJECTREMARK"].ToString();
                        //DR["CDATE"] = Convert.ToDateTime(DtItemDetails.Rows[i]["CDATE"].ToString()).ToString("dd-MMM-yyyy");
                        //DR["CTIME"] = Convert.ToDateTime(DtItemDetails.Rows[i]["CDATE"].ToString()).ToString("hh:mm:ss");
                        DTChildPO.Rows.Add(DR);
                    }
                }
                GrdPOChild.DataSource = DTChildPO;
                GrdPOChild.DataBind();
            }
        }
    }
    protected void btnshortcloseIssueitem_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in grdIssueItemShortClose.Rows)
        {
            CheckBox ChkItemStatus = (CheckBox)row.FindControl("ChkItem");
            HiddenField hiditemissue = (HiddenField)row.FindControl("hiditemissue");
            if (ChkItemStatus.Checked == true && ChkItemStatus.Enabled == true)
            {
                string SqlShortClose = "Update IVR_OnlineRegistrationSlip set ShortClose=1,ShortcloseDate=GETDATE(),shortclose_Remark='" + txtShortCloseRemarks.Text + "' where Regid='" + hiditemissue.Value + "'";
                int Update = DU.executesql(SqlShortClose);
                if (Update > 0)
                {
                    lblmsg.ForeColor = System.Drawing.Color.Green;
                    lblmsg.Text = "Short Close of Issued Item is Assigned.";
                    txtShortCloseRemarks.Text = "";
                }
            }
        }
        GetDataFromIssueItem();
    }    
    protected void ChkItem_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in grdIssueItemShortClose.Rows)
        {
            CheckBox ChkItemStatus = (CheckBox)row.FindControl("ChkItem");
            AjaxControlToolkit.ModalPopupExtender AJAXISSUE = (AjaxControlToolkit.ModalPopupExtender)row.FindControl("MPEISSUEITEM");
            if (ChkItemStatus.Checked == true && ChkItemStatus.Enabled == true)
            {
                AJAXISSUE.Show();
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        AjaxControlToolkit.ModalPopupExtender AJAXISSUE = (AjaxControlToolkit.ModalPopupExtender)grdIssueItemShortClose.Rows[0].FindControl("MPEISSUEITEM");
        AJAXISSUE.Hide();
        txtShortCloseRemarks.Text = "";
        GetDataFromIssueItem();
    }
}